﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WPF101_Data
{
    /// <summary>
    /// Interaction logic for BindingSecond.xaml
    /// </summary>
    public partial class BindingSecond : Window
    {
        public BindingSecond()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {   
            
            // textarea.Text = "The new value is reassigned";

            MySecondData data = this.Resources["mySecondData"] as MySecondData;
            data.Name = "Value is assigned by updating the object";

        }
    }

    // By implementing INotifyPropertyChanged interface, the update to the object will be 
    // reflected to the binding target.
    public class MySecondData : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        private string name;

        public MySecondData()
        {
            Name = "test";
        }

        public string Name {

            get
            {
                return name;
            }

            set
            {
                name = value;
                // Call OnPropertyChanged whenever the property is updated
                OnPropertyChanged("Name");
            }     
       }

        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        public override string ToString()
        {
            Random rd = new Random();
            return rd.Next().ToString();
        }


    }
}
